package com.zxlfysj.stack;

/**
 * 栈实现计算器
 * @author yangshujing
 * @create 2020-08-16 20:29
 */
public class Calculator {
    public static void main(String[] args) {

        String expression = "3-12*2/6-2*2";
        ArrayStack2 numStack = new ArrayStack2(10); //数栈
        ArrayStack2 operStack = new ArrayStack2(10); //符号栈

        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepNum = "";
        //循环扫描expression
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            //判断ch是数字还是运算符
            if(operStack.isOper(ch)) {
                if(!operStack.isEmpty()) {
                    //如果符号栈不空，就进行比较，如果当前的符号的优先级小于或等于栈中的符号，
                    //就需要pop出数栈两个数，再pop出符号栈的一个符号，进行运算
                    //运算结果再入数栈，当前符号入符号栈
                    while (!operStack.isEmpty() && operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                    }

                    operStack.push(ch);

                } else {
                    operStack.push(ch);
                }
            } else {
                //如果是数字，直接入数栈
                //numStack.push(ch - 48); //根据ascii码，减48才是真正的数字
                //处理多位数问题
                keepNum += ch;

                //如果ch是最后一个字符，直接入栈
                if(index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    //判断下一个字符是不是数字，如果是数字，继续扫描，如果是符号，当前字符入栈
                    if(operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = ""; //重置keepNum
                    }
                }

            }
            index++;
            if(index >= expression.length()) {
                break;
            }
        }

        //表达式扫描完毕，顺序从数栈和符号栈pop计算
        while (true) {
            if(operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);

        }
        System.out.printf("表达式 %s = %d", expression, numStack.pop());


    }
}


class ArrayStack2 {
    private int maxSize; //栈的大小
    private int[] stack; //使用数组模拟栈
    private int top = -1; //栈顶

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }

    //返回当前栈顶的值，不是出栈
    public int peek() {
        return stack[top];
    }

    //判断栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //判断栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈
    public void push(int value) {
        if(isFull()) {
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历
    public void list() {
        if(isEmpty()) {
            System.out.println("栈空");
            return;
        }
        for(int i = top; i >= 0; i--) {
            System.out.printf("stack[%d] = %d \n", i, stack[i]);
        }
    }

    //定义运算符的优先级，使用数字表示
    //数字越大，优先级越高
    //假定只有加减乘除
    public int priority(int oper) {
        if(oper == '*' || oper == '/') {
            return 1;
        } else if(oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    //判断是不是一个运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //计算方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;

        }
        return res;
    }
}
